package com.jxk.jms.security.controller;

import com.jxk.jms.security.util.VerifyCode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @ClassName : VerifyCodeController  //类名
 * @Description :   //描述
 * @Author : jxk  //作者
 * @Date: 2020-09-16 18:26  //时间
 */
@RestController
public class VerifyCodeController {
    @GetMapping("/verifyCode")
    public void code(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        /*一定需要设置*/
        resp.setContentType("image/jpeg");
        HttpSession session = req.getSession();
        /*hutools工具生成验证码*/
       /* ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
        String code = captcha.getCode();
        session.setAttribute("index_code", code);
        captcha.write(resp.getOutputStream());*/
        VerifyCode vc = new VerifyCode();
        BufferedImage image = vc.getImage();
        String text = vc.getText();
        session.setAttribute("index_code", text);
        VerifyCode.output(image, resp.getOutputStream());
    }
}